package com.TP.www.servlet;

import com.TP.www.po.Cart;
import com.TP.www.service.CartPort;
import com.TP.www.service.impl.CartImpl;

import java.util.ArrayList;
import java.util.List;

import static com.TP.www.constant.Constant.*;

/**
 * @author 邱彦祥
 * 购物车界面
 */
public class CartMenu {
    /**
     * 展示购物车界面主函数
     */
    public static void showCartMain(){
        while (true){
            String choose;
            System.out.println("***********购物车***********");
            List<Cart> carts = CartPort.getCartById(SELECT_FROM_CART,loginUser.getId());
            List<Cart> putAwayCarts = new ArrayList<>();
            List<Cart> pullOffCarts = new ArrayList<>();

            if(carts.isEmpty()){
                System.out.println("当前购物车内无商品!");
                System.out.println("***************************");
                System.out.println("按任意键返回!");
                SYS_SC.nextLine();
                return;
            }else {
                for (Cart cart:carts) {
                    if (cart.getState() == 1){
                        putAwayCarts.add(cart);
                    }else if(cart.getState() == -1){
                        pullOffCarts.add(cart);
                    }
                }
                if (!putAwayCarts.isEmpty()){
                    for (Cart cart : putAwayCarts){
                        System.out.println(cart.getStoreName() +" 商品名: " +
                                cart.getGoodName() +" 单价:"+ cart.getPrice() + " 数量:" +cart.getGoodsNum());
                    }
                }
                if(!pullOffCarts.isEmpty()){
                    System.out.println("下架商品：");
                    for (Cart cart : pullOffCarts){
                        System.out.println(cart.getStoreName() +" 商品名: " +
                                cart.getGoodName() +" 单价:"+ cart.getPrice() + " 数量:" +cart.getGoodsNum());
                    }
                }
            }
            System.out.println("***************************");
            System.out.println("请选择操作:");
            System.out.println("1.下单");
            System.out.println("2.删除商品");
            System.out.println("0.返回上级菜单");
            choose = SYS_SC.nextLine();
            switch (choose){
                case "0":
                    return;
                case "1":
                    showCartOrder(putAwayCarts);
                    break;
                case "2":
                    deleteCart();
                    break;
                default:
                    System.out.println("指令输入有误！请重新输入：");
            }
        }
    }

    /**
     * 下单界面
     * @param carts 购物车信息
     */
    public static void showCartOrder(List<Cart> carts){
        while (true) {
            String choose;
            int i = 1;
            System.out.println("***********下单界面***********");
            if(carts.isEmpty()) {
                System.out.println("当前购物车内无商品!!!(按任意键返回)");
                SYS_SC.nextLine();
                return;
            }
            for (Cart cart : carts){
                    System.out.println(i + " " +cart.getStoreName() +" 商品名: " +
                            cart.getGoodName() +" 单价:"+ cart.getPrice() + " 数量:" +cart.getGoodsNum());
                    i++;
            }
            System.out.println("***************************");
            System.out.println("请输入序号进行下单(按0返回上级界面):");
            choose = SYS_SC.nextLine();
            if(Integer.parseInt(choose) == 0){
                return;
            }
            while (Integer.parseInt(choose) >= i || Integer.parseInt(choose) < 0){
                System.out.println("输入有误!");
                System.out.println("请重新输入序号(按0返回上级界面):");
                if(Integer.parseInt(choose) == 0){
                    return;
                }
            }
            if(new CartImpl().order(loginUser,carts.get(Integer.parseInt(choose)-1))){
                return;
            }
        }
    }

    /**
     * 删除某条购物车信息
     */
    public static void deleteCart(){
        while(true){
            String choose;
            int i = 1;
            CartImpl cartimpl = new CartImpl();
            List<Cart> carts = CartPort.getCartById(SELECT_FROM_CART,loginUser.getId());
            if(carts.isEmpty()){
                System.out.println("当前购物车无商品!");
                System.out.println("按任意键返回...");
                SYS_SC.nextLine();
                return;
            }
            System.out.println("***********购物车***********");
            for (Cart cart:carts) {
                System.out.print( i + " "+cart.getStoreName() +" 商品名: " +
                        cart.getGoodName() +" 单价:"+ cart.getPrice() + " 数量:" +cart.getGoodsNum());
                if(cart.getState() == -1){
                    System.out.println(" 商品已下架!");
                }else if (cart.getState() == 1){
                    System.out.println(" ");
                }
                i++;
            }
            System.out.println("***************************");
            System.out.println("请输入序号进行删除(按0返回上级界面):");
            choose = SYS_SC.nextLine();
            if(Integer.parseInt(choose) == 0){
                return;
            }
            while (Integer.parseInt(choose) >= i || Integer.parseInt(choose) < 0){
                System.out.println("输入有误!");
                System.out.println("请重新输入序号(按0返回上级界面):");
                if(Integer.parseInt(choose) == 0){
                    return;
                }
            }
            if(cartimpl.delete(carts.get(Integer.parseInt(choose)-1))){
                System.out.println("删除成功!");
            }else {
                System.out.println("删除失败!");
            }
            System.out.println("按任意键继续...");
            SYS_SC.nextLine();
        }
    }
}
